﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace ivv
{
    public partial class relatorioLog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqld.Open();

                // carregar locais de origem para o ComBox
                string sqla = "select * from usuario order by nomeUsuario";

                SqlCommand cmda = new SqlCommand(sqla, sqld);
                SqlDataReader myReadera = cmda.ExecuteReader();

                ListItem aSelecione = new ListItem();
                aSelecione.Value = "0";
                aSelecione.Text = "Todos os usuários";
                lstUsuario.Items.Add(aSelecione);

                while (myReadera.Read())
                {
                    ListItem Usuario = new ListItem();
                    Usuario.Value = myReadera["codigoUsuario"].ToString();
                    Usuario.Text = myReadera["nomeUsuario"].ToString();
                    lstUsuario.Items.Add(Usuario);
                }
                // fim do carregamento
                myReadera.Close();
                cmda.Dispose();
                sqld.Close();

                txtData.Focus();
            }
        }

        protected void btnGerar_Click(object sender, EventArgs e)
        {
            string relatorio = "<table width=100% border=1 cellpadding=0 cellspacing=0 borderColor=#99CCFF>\n";
            relatorio += "<tr>\n";
            relatorio += "<th>Usuário</th>\n";
            relatorio += "<th>Descrição</th>\n";
            relatorio += "<th>Código</th>\n";
            relatorio += "<th>Data</th>\n";
            relatorio += "<th>Hora</th></tr>\n";


            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            string select = "SELECT codigoLog, codigoUsuario, descricaoLog, codigoEntidadeLog, dataLog, horaLog, ";
            select += "(SELECT nomeUsuario FROM usuario WHERE (codigoUsuario = [log].codigoUsuario)) AS usuario FROM [log]";

            if (txtData.Text != "")
            {
                select += " where dataLog = '" + txtData.Text + "' ";

                if (lstUsuario.SelectedValue != "0")
                {
                    select += " and codigoUsuario = " + lstUsuario.SelectedValue + " ";
                }
            }
            else
            {
                if (lstUsuario.SelectedValue != "0")
                {
                    select += " where codigoUsuario = " + lstUsuario.SelectedValue + " ";
                }
            }

            SqlCommand cmd = new SqlCommand(select, sqld);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    relatorio += "<tr>\n";
                    relatorio += "<td>" + dr["usuario"].ToString() + "</td>\n";
                    relatorio += "<td>" + dr["descricaoLog"].ToString() + "</td>\n";
                    relatorio += "<td>" + dr["codigoEntidadeLog"].ToString() + "</td>\n";
                    relatorio += "<td>" + dr["dataLog"].ToString() + "</td>\n";
                    relatorio += "<td>" + dr["horaLog"].ToString() + "</td>\n";
                    relatorio += "</tr>\n";
                }
            }

            dr.Close();

            sqld.Close();

            relatorio += "</table>\n";

            Panel1.Controls.Add(new LiteralControl(relatorio));
        }
    }
}
